<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/001.png" alt="">
        <span>秃头管理系统--gold_shit编写</span>
      </div>
      <el-button type="info" round @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggle">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#00a4ff" :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" router :default-active="navState">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" style="color:#fff;" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              {{item.authName}}
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <template slot="title"><i class="el-icon-aim"></i>{{subItem.authName}}</template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      navState: ''
    }
  },
  created () {
    this.getMentList()
    this.navState = window.sessionStorage.getItem('navActive')
  },
  methods: {
    logout () {
      sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('已退出')
    },
    // 获取所有菜单
    async getMentList () {
      const result = await this.$http.get('/menus')
      if (result.meta.status !== 200) return this.$message.error(result.meta.message)
      this.menuList = result.data
      // console.log(this.menuList)
    },
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (active) {
      window.sessionStorage.setItem('navActive', active)
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    width: 100%;
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    .el-button {
      width: 100px;
      height: 40px;
    }
    div {
      display: flex;
      align-items: center;
      img {
        width: 60px;
        height: 60px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .el-submenu__title {
      color: #fff !important;
    }
    .el-menu-item {
      color: #fff;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .iconfont {
    margin-right: 25px;
    margin-left: 3px;
  }
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 20px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
</style>
